﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Logic;

namespace web_CategoryMS.View.G01
{
    public partial class FS0103_Sub : System.Web.UI.Page
    {
        FS0103_SubLogic fS0103_SubLogic = new FS0103_SubLogic();
        string strMode { get { return Request["mode"] == null ? "" : Request["mode"].ToString(); }}
        string strAutoID { get { return Request["iAutoID"] == null ? "" : Request["iAutoID"].ToString(); } }

        #region 初始化事件
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    SetUp();
                }
                catch (Exception ex)
                {

                }
            }
        }
        #endregion

        #region 保存事件
        protected void btnSave_Click(object sender, EventArgs e)
        {
            #region 保存前校验
            string strRequirementNo = txtRequirementNo.Text.Trim();
            string strCategory = txtCategory.Text.Trim();
            string strRequirementName = txtRequirementName.Text.Trim();
            string strRequirementDescription = txtRequirementDescription.Text.Trim();
            byte[] imageData = null;
            string strRequirementPicName = "";
            if (string.IsNullOrEmpty(strRequirementNo))
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "", "alert('请输入需求编码！');", true);
            }
            if (string.IsNullOrEmpty(strCategory))
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "", "alert('请输入需求种类！');", true);
            }
            if (string.IsNullOrEmpty(strRequirementName))
            {
                ScriptManager.RegisterStartupScript(this, typeof(Page), "", "alert('请输入需求名称！');", true);
            }
            #region 校验图片文件
            if (File_Import.HasFile)
            {
                string[] CheckExcelExtensionArray = new string[] { ".jpg",".png" };
                string strUploadFileName = File_Import.FileName;
                string strExtension = Path.GetExtension(strUploadFileName).ToLower();
                bool _isImage = false;
                for (int i = 0; i < CheckExcelExtensionArray.Length; i++)
                {
                    if (strExtension == CheckExcelExtensionArray[i])
                    {
                        _isImage = true;
                        imageData = File_Import.FileBytes;
                        strRequirementPicName = File_Import.FileName;
                        break;
                    }
                }
                if (!_isImage)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('请上传图像文件!');", true);
                    return;
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('请上传图像文件!');", true);
                return;
            }
            #endregion

            #region 重复性校验
            if (strMode!= "edit" && fS0103_SubLogic.CheckExists(strRequirementNo))
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('编号已存在!');", true);
                return;
            }
            #endregion

            #endregion

            #region 保存
            if (strMode== "edit")
            {
                fS0103_SubLogic.Modify(strAutoID,strRequirementNo, strCategory, strRequirementName, strRequirementDescription, imageData, strRequirementPicName);
            }
            else
            {
                fS0103_SubLogic.Save(strRequirementNo, strCategory, strRequirementName, strRequirementDescription, imageData, strRequirementPicName);
            }
            

            ScriptManager.RegisterStartupScript(this, typeof(Page), "", "window.opener.recall();window.close();", true);
            #endregion
        }
        #endregion

        #region 初始化方法
        public void SetUp()
        {
            if (strMode == "edit")
            {
                DataTable dt = fS0103_SubLogic.getRequirement(strAutoID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    this.txtRequirementNo.Text = dt.Rows[0]["vcRequirementNo"].ToString();
                    this.txtCategory.Text = dt.Rows[0]["vcCategory"].ToString();
                    this.txtRequirementName.Text = dt.Rows[0]["vcRequirementName"].ToString();
                    this.txtRequirementDescription.Text = dt.Rows[0]["vcRequirementDescription"].ToString();
                }
                btnDownLoadImage.Visible = true;
            }
        }
        #endregion

        #region 下载源图片
        protected void btnDownLoadImage_Click(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = fS0103_SubLogic.getRequirement(strAutoID);
                if (dt != null && dt.Rows.Count > 0)
                {
                    string fileName = dt.Rows[0]["vcRequirementPicName"].ToString();//客户端保存的文件名
                    Response.ContentType = "application/octet-stream";
                    //通知浏览器下载文件而不是打开
                    Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    Response.BinaryWrite((byte[])dt.Rows[0]["vcRequirementPic"]);
                    Response.Flush();
                    Response.End();
                }
                
            }
            catch (Exception ex)
            {

            }
        }
        #endregion
    }
}